package com.ctgu.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ctgu.server.dao.constant.CacheKey;
import com.ctgu.server.dao.dto.AttendanceLocationDto;
import com.ctgu.server.dao.entity.AttendanceGroupUser;
import com.ctgu.server.dao.entity.AttendanceLocation;
import com.ctgu.server.dao.vo.Result;
import com.ctgu.server.mapper.AttendanceLocationMapper;
import com.ctgu.server.service.AttendanceLocationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctgu.server.utils.ResultUtil;
import com.github.dozermapper.core.Mapper;
import net.oschina.j2cache.CacheChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * <p>
 * 考勤地点表 服务实现类
 * </p>
 *
 * @author ctgu
 * @since 2023-06-13
 */
@Service
public class AttendanceLocationServiceImpl extends ServiceImpl<AttendanceLocationMapper, AttendanceLocation> implements AttendanceLocationService {
    //打卡地点数量限制
    private static final int Location_Size=5;
    @Autowired
    private Mapper mapper;
    @Autowired
    private AttendanceGroupUserServiceImpl attendanceGroupUserService;

    @Autowired
    private CacheChannel cacheChannel;
    @Override
    public Result addAttendanceLocation(AttendanceLocationDto attendanceLocationDto) {
        //查询考勤地点是否超出范围
        LambdaQueryWrapper<AttendanceLocation> locationLambdaQueryWrapper=new LambdaQueryWrapper<>();
        locationLambdaQueryWrapper.eq(AttendanceLocation::getAttendanceGroupId,attendanceLocationDto.getAttendanceGroupId());
        if(attendanceLocationDto.getAttendanceLocationList().size()+ this.count(locationLambdaQueryWrapper)>Location_Size){
            return ResultUtil.error("每个考勤组总共考勤地点上限不能超过"+Location_Size+"!");
        }
        if(!attendanceGroupUserService.is_admin(attendanceLocationDto.getUserId(),attendanceLocationDto.getAttendanceGroupId())){
            return ResultUtil.error("用户权限不足");
        }
        //修改考勤地点
        String a=attendanceLocationDto.getAttendanceGroupId();
        List<AttendanceLocation> attendanceLocationList= attendanceLocationDto.getAttendanceLocationList();
        attendanceLocationList.stream().map(l->{
            l.setAttendanceGroupId(a);
            return null;
        });
        boolean flag= this.saveBatch(attendanceLocationList,attendanceLocationList.size());
        if(!flag){
            return ResultUtil.error("系统繁忙，插入失败");
        }
        return ResultUtil.success("请求成功");



    }

    @Override
    public Result deleteAttendanceLocation(String userId, String attGrId, String locationId) {
        if(!attendanceGroupUserService.is_admin(userId,attGrId)){
            return ResultUtil.error("用户权限不足");
        }
        this.removeById(locationId);
        return ResultUtil.success("请求成功");
    }

    @Override
    @Transactional
    public Result deleteAll(String userId, String attGrId) {
        if(!attendanceGroupUserService.is_admin(userId,attGrId)){
            return ResultUtil.error("用户权限不足");
        }
        LambdaQueryWrapper<AttendanceLocation> locationLambdaQueryWrapper=new LambdaQueryWrapper<>();
        locationLambdaQueryWrapper.eq(AttendanceLocation::getAttendanceGroupId,attGrId);
        this.remove(locationLambdaQueryWrapper);
        return ResultUtil.success("操作成功");
    }


    /**
     * @param userId:
     * @param attGrId:
     * @return List<AttendanceLocation>
     * @author kvzjj
     * @description TODO 返回打卡地点信息
     * @date 2023/6/13 17:07
     */
    @Override
    public List<AttendanceLocation> listGroupAll(String userId, String attGrId) {
        //判断用户是否在当前组织中
        LambdaQueryWrapper<AttendanceGroupUser> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AttendanceGroupUser::getUserId,userId).eq(AttendanceGroupUser::getGroupId,attGrId);
        if(attendanceGroupUserService.count(lambdaQueryWrapper)==0){
            return null;
        }
        //若该用户在当前打卡表则返回信息  从缓存中查看
        List<AttendanceLocation> attendanceLocationList= (List<AttendanceLocation>) cacheChannel.get(CacheKey.ATTENDANCE_LOCATION,attGrId).getValue();
        if(attendanceLocationList==null){
            LambdaQueryWrapper<AttendanceLocation> locationLambdaQueryWrapper=new LambdaQueryWrapper<>();
            locationLambdaQueryWrapper.eq(AttendanceLocation::getAttendanceGroupId,attGrId);
            attendanceLocationList=this.list(locationLambdaQueryWrapper);
            cacheChannel.set(CacheKey.ATTENDANCE_LOCATION,attGrId,attendanceLocationList);
        }
        return attendanceLocationList;
    }



}
